import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { AuthService } from './auth.service';
import { InfoResponse } from './dto/info.dto';
import {
  LoginRequestBody,
  LoginResFailure,
  LoginResponse,
} from './dto/login.dto';
import { JwtAuthGuard } from './jwt-auth.guard';

@ApiTags('auth')
@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Post('login')
  async login(@Body() body: LoginRequestBody) {
    const { username, password } = body;
    const payload = await this.authService.validateUser(username, password);
    if (payload) {
      const token = await this.authService.login(payload);
      return new LoginResponse(token);
    } else {
      return new LoginResFailure();
    }
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Get('info')
  async info() {
    return new InfoResponse();
  }
}
